//addressnew.js
let QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
let qqmapsdk = new QQMapWX({
    key: '4ZPBZ-JXJCX-M5O4R-ZOQDD-7LB6T-XGFET'
}); 

Page({
  data: {
    region: [],
    customItem: '请选择',
    modifySex: false,
    sexName: '请选择性别',
    sexcode:'',
    nimationDatas: {},
    addressarea:''
  },
  onLoad: function () {
    
  },
  chooseSex: function () {
    let that = this;
    that.setData({
      modifySex: true
      }, () => {
        let animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease'
        })
         that.animation = animation 
         setTimeout(function(){
           that.bigs();
        },200)
    })
  },
   modifySexCancel: function () {
    let that = this;
    that.setData({
      modifySex: false
      }, () => {
        let animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease'
        })
         that.animation = animation 
         setTimeout(function(){
           that.smalls();
        },200)
    })
  },
   bigs: function () {
    let that = this;
    that.animation.scale(1).step()
    that.setData({
      animationDatas: that.animation.export()
    })
  },
  smalls: function () {
    let that = this;
    that.animation.scale(0).step()
    that.setData({
      animationDatas: that.animation.export()
    })
  },
  xzSex: function (e) {
    let that = this;
    let sexId = e.currentTarget.dataset.sex;
    if (sexId == '0') {
      that.setData({
        sexName: '男',
        modifySex: false,
        sexcode:'0'
      }, () => {
        let animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease'
        })
         that.animation = animation 
         setTimeout(function(){
           that.smalls();
        },200)
      })
    } else {
      that.setData({
        sexName: '女',
        modifySex: false,
        sexcode:'1'
      }, () => {
        let animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'ease'
        })
         that.animation = animation 
         setTimeout(function(){
           that.smalls();
        },200)
      })
    }
  },
  dzPhone:function (e) {
    let dzphone = e.detail.value;
    if(dzphone.length == 11){
      let telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
      if (!(telStr.test(dzphone))) {
        wx.showToast({
          title: '请输入有效的手机号',
          icon: 'none',
          duration: 1500
        })
      }
    }
  },
  chooseArea: function (e) {
    let that = this;
    that.setData({
      region: e.detail.value
    })
  },
  intelLigence: function () {
    let that = this;
    if (wx.canIUse('button.open-type.openSetting')) {
        wx.getSetting({
          success: (res) => {
            if (res.authSetting['scope.userLocation']) {
                wx.getLocation({
                  type: 'gcj02',
                  success: (res) => {
                    if (res.errMsg == "getLocation:ok") {
                      qqmapsdk.reverseGeocoder({
                        location: {
                          latitude: res.latitude,
                          longitude: res.longitude
                        },
                        success: function (data) {
                          console.log(data)
                          let specific = data.result.address + data.result.formatted_addresses.recommend;
                          that.setData({
                            addressarea:specific
                          })
                        }
                      })
                    }
                  }
                })
              } else {
                wx.getLocation({
                  type: 'gcj02',
                  success: (res) => {
                    if (res.errMsg == "getLocation:ok") {
                      console.log(res.longitude, res.latitude)
                    }
                  }
                })
              }
          }
      })
    } else {
          wx.showModal({
            title: '提示',
            content: '微信版本过低，无法使用该功能,谢谢！',
            showCancel: false,
            confirmColor: '#fc0636'
          })
        }
  },
  addAdress:function(e){
    console.log(e)
    let adressData = e.detail.value;
    if (adressData.xm != '') {
      if (adressData.sex != '') {
        if (adressData.phonenum != '') {
          if (adressData.location != '') {
            if (adressData.addressarea != '') {
                wx.showToast({
                  title: '添加成功',
                  icon: 'success',
                  duration: 1500,
                })
                 wx.navigateBack({
                     delta: 1
                 })
            } else {
               wx.showModal({
                title: '',
                content: '请填写详细地址',
                showCancel:false,
                confirmColor:'#fc0636'
              })
            }
          } else {
            wx.showModal({
              title: '',
              content: '请选择所在地区',
              showCancel:false,
              confirmColor:'#fc0636'
            })
          }
        } else {
          wx.showModal({
            title: '',
            content: '请填写手机号',
            showCancel:false,
            confirmColor:'#fc0636'
          })
        }
      } else {
        wx.showModal({
          title: '',
          content: '请选择性别',
          showCancel:false,
          confirmColor:'#fc0636'
        })
      }
    } else {
      wx.showModal({
        title: '',
        content: '请填写姓名',
        showCancel:false,
        confirmColor:'#fc0636'
      })
    }
  }
})
